import {reqGetUserAddress,reqGetTradeDetail,reqSubmitOrder} from '@/api'

const state = {
   addressInfo:[],
   tradeDetailInfo:{},
   orderNum:''
}

const mutations = {
    GETUSERADRESS(state,addressInfo){
        state.addressInfo = addressInfo
    },
    GETTRADEDETAIL(state,tradeDetailInfo){
        state.tradeDetailInfo = tradeDetailInfo
    },
    SUBMITORDER(state,orderNum){
        state.orderNum = orderNum
    }
}
const actions = {
    // 获取用户地址信息
    async getUserAddress({commit}){
        let result = await reqGetUserAddress()
        // console.log(result)
        if(result.code == 200){
            commit('GETUSERADRESS',result.data)
            return 'ok'
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 获取订单交易页信息
    async getTradeDetail({commit}){
        let result = await reqGetTradeDetail()
        console.log(result)
        if(result.code == 200){
            commit('GETTRADEDETAIL',result.data)
            return 'ok'
        }else{
            return Promise.reject(new Error('faile'))
        }
    },
    // 提交订单
    async submitOrder({commit},{params,tradeNo}){
      let result =  await reqSubmitOrder(tradeNo,params)
      console.log(result)
      if(result.code == 200){
          commit('SUBMITORDER',result.data)
          return 'ok'
      }else{
        return Promise.reject(new Error('faile'))
      }
    }
}
const getters = {
    detailArrayList(){
        return state.tradeDetailInfo.detailArrayList || []
    }
}
export default {
    state,
    mutations,
    actions,
    getters
}